/*
 *    Copyright (c) 2018-2025, 云集汇通 All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yunjihuitong.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 */

package com.yjht.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yjht.common.constant.Constant;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.Query;
import com.yjht.modules.sys.dao.SysDictDao;
import com.yjht.modules.sys.entity.SysDictEntity;
import com.yjht.modules.sys.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        int count = this.selectCount(new EntityWrapper<SysDictEntity>());
        params.put("limit",count+"");
        Page<SysDictEntity> page = this.selectPage(
                new Query<SysDictEntity>(params).getPage(),
                new EntityWrapper<SysDictEntity>()
                    .like(StringUtils.isNotBlank(name),"name", name)
        );
        List<SysDictEntity> list = page.getRecords();
        List<SysDictEntity> result = new ArrayList<>();
        if(list != null && list.size() > 0){
            for(SysDictEntity entity : list){
                if(Constant.top_dict_id == entity.getParentid()){
                    result.add(entity);
                }
            }
        }
        if(result.size() > 0){
            for(SysDictEntity dict : result){
                for(SysDictEntity entity : list){
                    if(dict.getId() == entity.getParentid()){
                        dict.addChildren(entity);
                    }
                }
            }
        }
        page.setRecords(result);
        return new PageUtils(page);
    }

    @Override
    public List<SysDictEntity> queryList(SysDictEntity entity) {
        EntityWrapper<SysDictEntity> ew = new EntityWrapper<>();
        ew.setEntity(entity);
        SysDictEntity dict = this.selectOne(ew);
        Long id = dict.getId();
        SysDictEntity tmp = new SysDictEntity();
        tmp.setParentid(id);
        ew.setEntity(tmp);
        List<SysDictEntity> list = this.selectList(ew);
        return list;
    }

}
